package com.logicwin.aibala.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipException;

import com.logicwin.aibala.model.androidSWInfo.APKListItem;
import com.logicwin.aibala.model.androidSWInfo.AndroidSWInfoService;
import com.logicwin.aibala.services.file.FileConnection;
import com.logicwin.aibala.services.file.ImageManager;
import com.logicwin.aibala.services.net.NetConnection;
import com.logicwin.aibala.services.xml.XMLParser;
import com.logicwin.aibala.services.zip.ZipUtil;
import com.logicwin.aibala.utils.MyLog;

public class LoadStartUpAction implements Runnable {
	
	private final static String tag = "LoadStartUpAction";
	
	private String getListUrl = "http://home.kequan001.com/aibala/getStartUp.php";

	private LoadStartUpListener _listener = null;

	public boolean startLoad(LoadStartUpListener listener) {
		_listener = listener;
		new Thread(this).start();
		return true;
	}

	@Override
	public void run() {
		String zipPath = NetConnection.httpGet(getListUrl, null);
		_listener.loadProgress(6);
		NetConnection.downLoadFile(zipPath,
				FileConnection.getCurrentPath() + "/temp.zip");
		_listener.loadProgress(10);
		try {
			ZipUtil.upZipFile(new File(FileConnection.getCurrentPath()
					+ "/temp.zip"), FileConnection.getCurrentPath());
		} catch (ZipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		_listener.loadProgress(6);
		
		String[] allClass = new String[] { AndroidSWInfoService.CLASS_ID_INDEX,
				AndroidSWInfoService.CLASS_ID_GAME,
				AndroidSWInfoService.CLASS_ID_ENTER,
				AndroidSWInfoService.CLASS_ID_SNS,
				AndroidSWInfoService.CLASS_ID_MEDIA,
				AndroidSWInfoService.CLASS_ID_OFFICE,
				AndroidSWInfoService.CLASS_ID_SYS };
		String[] allSort = new String[] { 
				AndroidSWInfoService.SORT_ID_BEST,
				AndroidSWInfoService.SORT_ID_NEW,
				AndroidSWInfoService.SORT_ID_RANK };

		for (String classType : allClass) {
			for (String sortType : allSort) {
				_listener.loadProgress(2);
				String xmlPath = "softlist_"+classType+"_"+sortType+".xml";
				List<APKListItem> apkList = XMLParser.parseList(FileConnection
							.getCurrentPath() + "/" + xmlPath);
				AndroidSWInfoService.setAPKList(classType,
							sortType, 0, apkList);
				_listener.loadProgress(2);
			}
		}
		
		_listener.loadDone();
	}
}
